﻿using System;
using System.Collections.Generic;
using System.Text;
using WindowsInput;
using WindowsInput.Native;
using System.Net;
using System.Net.Security;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;

namespace Tornado
{
    public class SignalProcesserFile : SignalProcesser
    {
        private FileReceiver fRec = null;
        public override SignalCodes Code
        {
            get { return SignalCodes.FILE_TRANSFER; }
        }

        public override void Process(ref SignalPacket p)
        {
            switch ((FileExCode)p.ExtensionCode)
            {
                case FileExCode.Address:
                    fRec = new FileReceiver();
                    fRec.SetAddress(p.Argument);
                    break;
                case FileExCode.Port:
                    fRec.SetPort(p.Argument);
                    break;
                case FileExCode.Open:
                    fRec.SetOpenFile(p.Argument);
                    fRec.StartReceive();
                    break;
            }

        }

        class FileReceiver
        {
            private IPAddress addr;
            private int port;
            private bool openFile;

            public void SetAddress(byte[] address)
            {
                addr = new IPAddress(address);
            }

            public void SetPort(byte[] p)
            {
                port = BitConverter.ToInt32(p, 0);
            }

            public void SetOpenFile(byte[] b)
            {
                openFile = (b[0] != 0);
            }

            public void StartReceive()
            {
                string args = addr.ToString() + " " + port.ToString() + " " + openFile.ToString();
                System.Diagnostics.Process.Start(Application.ExecutablePath, args);
            }
        }
    }
}
